package com.jy.appmarket.model;

import android.os.Parcel;
import android.os.Parcelable;

import com.jy.app.market.apk.type.LinkType;
import com.jy.app.market.idata.data.Banner;

public class BannerInfo extends Banner implements Parcelable {
	private static final long serialVersionUID = 889374074693879531L;

	@Override
	public int describeContents() {
		return 0;
	}

	public static int fromType(LinkType type) {
		if (type == null) {
			return 0;
		}
		if (type == LinkType.WebView) {
			return 1;
		}

		if (type == LinkType.Cards) {
			return 2;
		}
		return 0;
	}

	public static LinkType toType(int value) {
		if (value == 1) {
			return LinkType.WebView;
		}
		if (value == 2) {
			return LinkType.Cards;
		}
		return LinkType.App;

	}

	public static BannerInfo fromBanner(Banner b) {
		BannerInfo bi = new BannerInfo();
		if (b == null) {
			return bi;
		}
		bi.setImageUrl(b.getImageUrl());
		bi.setTitle(b.getTitle());
		bi.setType(b.getType());
		bi.setUrl(b.getUrl());
		return bi;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(getImageUrl());
		dest.writeString(getTitle());
		dest.writeString(getUrl());
		dest.writeInt(fromType(getType()));
	}

	public static final Parcelable.Creator<BannerInfo> CREATOR = new Parcelable.Creator<BannerInfo>() {
		@Override
		public BannerInfo createFromParcel(Parcel source) {
			BannerInfo b = new BannerInfo();
			b.setImageUrl(source.readString());
			b.setTitle(source.readString());
			b.setUrl(source.readString());
			b.setType(toType(source.readInt()));
			return b;
		}

		@Override
		public BannerInfo[] newArray(int size) {
			return new BannerInfo[size];
		}
	};
}
